package com.jiaquyun.jcyx.main.mine

import android.graphics.Typeface
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ConvertUtils
import com.jiaquyun.jcyx.R
import com.jiaquyun.jcyx.jetpack.mode.UserViewModel
import com.module.lemlin.custom.SpacingItemDecoration
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_mine_purchase.*

class MinePurchaseActivity : OwnerViewModeActivity<UserViewModel>() {

    companion object {
        fun open() {
            ActivityUtils.startActivity(MinePurchaseActivity::class.java)
        }
    }

    private val mPurchaseGoodsAdapter = PurchaseGoodsAdapter()

    override fun viewModel(): UserViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine_purchase

    override fun stateBar(): StatusBar {
        return StatusBar(isLightMode = true, color = R.color.white, view = flMinePurchaseBar)
    }

    override fun initView() {

        ivAppBack.setOnClickListener { finish() }

        rvMinePurchaseBody.apply {
            val decoration = SpacingItemDecoration(
                vGap = ConvertUtils.dp2px(10f),
                hGap = ConvertUtils.dp2px(10f)
            )
            addItemDecoration(decoration)
            itemAnimator?.changeDuration = 0
        }.adapter = mPurchaseGoodsAdapter

        mSmartRefreshLayout.setEnableRefresh(false)
        mSmartRefreshLayout.setEnableLoadMore(false)
        mSmartRefreshLayout.setOnLoadMoreListener {
            viewModel.userPurchaseList(isLoadMore = true)
        }
    }

    override fun initData() {
        viewModel.userPurchaseList()
    }

    override fun initViewMode() {


        viewModel.purchaseGoodsLiveDate.observe(this as LifecycleOwner, Observer { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg ?: "位置错误")
                mSmartRefreshLayout.finishLoadMore((false))
            } else if (response.status == Status.SUCCESS) {
                mSmartRefreshLayout.finishLoadMore()
            }
            if (response.status != Status.SUCCESS)
                return@Observer
            response.data?.let {
                val data = it.data
                data.clear()
                if (data.size == 0) {
                    mPurchaseGoodsAdapter.setEmptyView(createEmptyView())
                }
                if (it.loadMore) {
                    mPurchaseGoodsAdapter.addData(data)
                } else {
                    mPurchaseGoodsAdapter.setNewInstance(data)
                }
                if (data.size < viewModel.rows) {
                    mPurchaseGoodsAdapter.setFooterView(createFooterView())
                    mSmartRefreshLayout.setEnableLoadMore(false)
                } else {
                    mPurchaseGoodsAdapter.removeAllFooterView()
                    mSmartRefreshLayout.setEnableLoadMore(true)
                }
            }
        })
    }

    private fun createEmptyView(): View {
        return layoutInflater.inflate(R.layout.view_empty_error, null)
    }

    private fun createNullDataView(): View {
        return layoutInflater.inflate(R.layout.view_empty_error, null).apply {
            findViewById<TextView>(R.id.tvEmptyMessage).text = "空空如也哦~"
        }
    }

    private fun createFooterView(): View = TextView(this).apply {
        layoutParams = FrameLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            ConvertUtils.dp2px(30f)
        )
        gravity = Gravity.CENTER
        textSize = 13f
        text = "没有更多商品"
        setTypeface(null, Typeface.BOLD)
        setTextColor(ColorUtils.getColor(R.color.FF_666666))
    }
}